package sharemem

import (
	"encoding/json"
	"server_cluster/logic/game_config"
	"time"

	"server_cluster/common/gdconf"
)

// Index结构全部由GC管理

type Item_Index struct {
	Normal_map  map[int32]int16
	Special_map map[int32]int16
}

type Quest_Index struct {
	SubQuestMap4Cond     map[int16]*[]*Quest        //map[subType]*[]*Quest
	QuestMap             map[int32]map[int32]*Quest //map[ParentQuestId]map[questId]*Quest
	ParentQuestMap       map[int32]*ParentQuest     //map[parentQuestId]*ParentQuest
	ParentQuestFinishMap map[int32]bool
}

type Shop_Index struct {
	ShopBuyRecord map[int32]*ShopGoods
}

type Equip_Index struct {
	LastComposeEquip *Equip // 上一次合成的装备 在线数据
	EquipMap         map[int32]*Equip
	EquipJewelMap    map[int32]*EquipJewel
}

type Role_Index struct {
	RoleMap map[int32]*Role
}

type Scene_Index struct {
	LastFishInfo *FishInfo
	SceneMap     map[int32]*Scene
	MonsterMap   map[int32]map[int32]*Monster
	GadgetMap    map[int32]map[int32]*Gadget
	NpcListMap   map[int32]map[int32]*Npc
}

type Codex_Index struct {
	CodexMap map[int32]*Codex
}

type Weapon_Index struct {
	WeaponMap map[uint64]*Weapon
}

type Home_Index struct {
	BuildingMap        map[uint64]*Building
	UnlockBuildingList map[int32]*UnlockBuilding
}

type Activity_Index struct {
	DungeonActivityMap map[int32]*DungeonActivity
	DailyTaskMap       map[int32]*Task
	WeeklyTaskMap      map[int32]*Task
	SignInMap          map[int32]*SignIn
	ZpMap              map[int32]*Zp
}

// User_Index 玩家数据索引
type User_Index struct {
	Shm_user      *User          // user对象
	Item          Item_Index     // 道具
	Quest         Quest_Index    // 任务
	ShopBuyRecord Shop_Index     // 商店
	Equip         Equip_Index    // 装备
	Role          Role_Index     // 角色
	Scene         Scene_Index    // 场景
	Codex         Codex_Index    // 图鉴
	Weapon        Weapon_Index   // 武器
	Home          Home_Index     // 家园
	Activity      Activity_Index // 活动
}

// Index 全局索引对象
type Index struct {
	Cfg     Config_Index
	UserMap map[int64]*User_Index // 玩家map key:uid value:User指针
}

// 进程内全局变量
var index_game Index

// 玩家map

func GetUserMap() map[int64]*User_Index { return index_game.UserMap }

func AddUser(uid int64, user *User) {
	if index_game.UserMap == nil {
		index_game.UserMap = make(map[int64]*User_Index)
	}
	index_game.UserMap[uid] = &User_Index{
		Shm_user:      user,
		Quest:         Quest_Index{},
		Item:          Item_Index{},
		ShopBuyRecord: Shop_Index{},
	}
}

func DelUser(uid int64) {
	delete(index_game.UserMap, uid)
}

// 初始化用户map

func InitUserMap() {
	for i := 0; i < game_config.USER_MAX; i++ {
		if MyShm.Usr[i].IsUse() {
			AddUser(MyShm.Usr[i].GetUid(), &(MyShm.Usr[i]))
			if MyShm.Usr[i].IsOnline {
				MyShm.Usr[i].UserDataInit()
			} else {
				MyShm.Usr[i].LoadUserMap()
			}
		}
	}
}

// 配置表map

type Config_Index struct {
	RewardMap map[int32]int16
}

// InitConfigMap 初始化配置表map
func InitConfigMap() {
	gdconf.CONF = new(gdconf.GameDataConfig)
	buffer := MyShm.Cfg[:MyShm.CfgLen]
	err := json.Unmarshal(buffer, &gdconf.CONF.GameDataMaps)
	if err != nil {
		panic(err)
	}
}

func CheckUserMap() {
	for {
		var keyList = make([]int64, 0)
		for key, value := range GetUserMap() {
			if value.Shm_user.GetUid() == 0 {
				keyList = append(keyList, key)
			}
		}
		for _, key := range keyList {
			delete(GetUserMap(), key)
		}
		time.Sleep(time.Second * 10)
	}
}
